﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;

public partial class edit_guest : System.Web.UI.Page
{
    OleDbConnection conx;
    string pk;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        //GridView1.SelectedIndex
        if (GridView1.SelectedIndex != -1)
        {
            pk = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            Label5.Text = "";
        }
    }
    protected void Edit_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex == -1)
        {
            Label5.Text = "\"Please Seleted Row !\"";
            return;
        }
        else
        {
            pk = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            Label3.Text = pk;
            MultiView1.ActiveViewIndex = 1;

            TextBox1.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
            TextBox2.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text;
            TextBox3.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
            TextBox4.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text;
        }

    }
    protected void Save_Click(object sender, EventArgs e)
    {
        try
        {
            String con = "Provider=MSDAORA;Data Source=orcl;Password=oracle;User ID=pearl";//"DSN=orcl;Driver={Microsoft ODBC for Oracle};UID=system;PWD=password;SERVER=localhost;";
            conx = new OleDbConnection(con);
            conx.Open();

            if (conx.State == ConnectionState.Open)
            {
                pk = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
                string guestTypeId = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text;
                string ddl1 = DropDownList1.SelectedValue;
                string tb1 = TextBox1.Text;
                string tb2 = TextBox2.Text;
                string tb3 = TextBox3.Text;
                string tb4 = TextBox4.Text;

                OleDbCommand ODC = new OleDbCommand("UPDATE GUESTS SET PRENAME_ID = '" + DropDownList1.SelectedValue + "' , GUEST_FNAME = '" + TextBox1.Text + "', GUEST_LNAME = '" + TextBox2.Text + "' , ADDRESS = '" + TextBox3.Text + "' , GUEST_TYPE_ID = '" + guestTypeId + "' , GUEST_TEL = '" + TextBox4.Text + "' WHERE GUEST_ID = '" + pk + "'", conx);
                ODC.ExecuteNonQuery();
                OleDbCommand ODC2 = new OleDbCommand("COMMIT", conx);
                ODC2.ExecuteNonQuery();
                Label1.Text = "\"GUESTS No." + pk + "  DATA UPDATED\"";

                Label3.Text = "";
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
            }
            else
                Label1.Text = "Oracle Connect Failed";
        }

        catch (Exception ex)
        {
            Label1.Text = ex.Message + "<br />" + ex.StackTrace;
        }

        finally
        {
            conx.Close();
            conx = null;
        }
    }
    protected void Back_Click(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 1)
        {
            MultiView1.ActiveViewIndex = 0;
        }
        else
            Response.Redirect("guest.aspx");
    }
}